package cn.com.yinhex.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Properties;
import org.springframework.core.io.ClassPathResource;
/**
 * 通过product.properties获得相应属性
 */
public class DBConfig {

  private static final String CONFIG_FILE = "project.properties";

  private static DBConfig instance = null;

  private static HashMap row = null;

  /**
   * 私有构造，不能直接new
   */
  private DBConfig() {
    InputStream in = null;
    try {
    	Properties properties = new Properties();
//    	String svndir = Config.class.getResource("./").toString();
//        svndir = svndir.split("WEB-INF")[0];
//        svndir = svndir.replaceFirst("file:/", "");
//        System.out.println("svndir========="+svndir+CONFIG_FILE);
  		ClassPathResource res=new ClassPathResource(CONFIG_FILE);
      in = res.getInputStream();
      properties.load(in);
      if(properties.size()!=0){
      	row=new HashMap(properties);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        if (in != null) {
          in.close();
        }
      } catch (Exception e) {
      	e.printStackTrace();
      }
    }
  }

  /**
   * 初始化
   * @return
   */
  public static DBConfig init() {
    if (instance == null) {
      instance = new DBConfig();
    }
    return instance;
  }

  /**
   * 获得Row
   * @return
   */
  public HashMap getRow(){
  	return this.row;
  }
  
  /**
   * @param configKey
   * @return
   */
  public String gets(String name) {
      try {
          if (row.get(name) != null)
              return row.get(name).toString();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  
  public String gets(String name,String defaultStr) {
      try {
          if (row.get(name) != null)
              return row.get(name).toString();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return defaultStr;
  }

  public int geti(String name) {
      return Integer.parseInt(gets(name,"0"));
  }

}
